#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int a[N], q[N];

int main() {
	int n, k;
	cin >> n >> k;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	int hh = 0, tt = -1;
	for (int i = 0; i < n; ++i) {
		if (hh <= tt && i + 1 - k > q[hh]) {
			hh++;
		}
		while (hh <= tt && a[q[tt]] >= a[i]) {
			tt--;
		}
		q[++tt] = i;
		if (i + 1 >= k) {
			cout << a[q[hh]] << ' ';
		}
	}
	cout << endl;

	hh = 0, tt = -1;
	for (int i = 0; i < n; ++i) {
		if (hh <= tt && i + 1 - k > q[hh]) {
			hh++;
		}
		while (hh <= tt && a[q[tt]] <= a[i]) {
			tt--;
		}
		q[++tt] = i;
		if (i + 1 >= k) {
			cout << a[q[hh]] << ' ';
		}
	}
	cout << endl;
	return 0;
}